//
//  ViewController.m
//  CoreAnimationDemo
//
//  Created by ZJ on 2018/8/10.
//  Copyright © 2018年 iCatch Technology Inc. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, weak) CALayer *layer;
@property (nonatomic, weak) CALayer *layer1;

@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (nonatomic, assign) NSInteger imageNum;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
//    [self basicAnimationTest];
//    [self keyframeAnimationTest];
//    [self animationGroupTest];
    [self keyframeAnimationAdvanceTest];
    
    self.imageNum = 0;
}

#pragma mark - 转场动画 (CATransition)
// 轻扫手势执行的方法
- (IBAction)imageChange:(UISwipeGestureRecognizer *)sender {
    self.imageNum++;
    
    self.imageNum == 5 ? self.imageNum = 0 : self.imageNum;
    
    self.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld", self.imageNum + 1]];
    
    // 转场动画
    // 1.创建动画
    CATransition *anim = [[CATransition alloc] init];
    
    // 2.操作
    anim.type = @"moveIn"; // 动画类型
    
    if (sender.direction == UISwipeGestureRecognizerDirectionLeft) {
        // 从右向左
        // 方向
        anim.subtype = kCATransitionFromRight;
    } else if (sender.direction == UISwipeGestureRecognizerDirectionRight) {
        // 从左向右
        anim.subtype = kCATransitionFromLeft; // 动画方向
    }
    
    // 3.添加动画
    [self.imageView.layer addAnimation:anim forKey:nil];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    [self keyframeAnimation];
//    [self animationGroup];
    [self keyframeAnimationAdvance];
}

#pragma mark - 组动画(CAAnimationGroup)
- (void)animationGroupTest {
    UIView *redView = [[UIView alloc] init];
    
    redView.frame = CGRectMake(100, 100, 20, 20);
    redView.backgroundColor = [UIColor redColor];
    
    self.layer = redView.layer;
    
    [self.view addSubview:redView];
}

// 组动画
- (void)animationGroup {
    // 1.创建动画
    CAAnimationGroup *group = [[CAAnimationGroup alloc] init];
    
    // ------ 基本动画(自旋转) ------
    // 1.创建动画对象(做什么动画)
    CABasicAnimation *bAnim = [[CABasicAnimation alloc] init];
    
    // 2.怎么做动画
    bAnim.keyPath = @"transform.rotation";
    bAnim.byValue = @(2 * M_PI * 5); // 在自身的基础上增加
    
    // ------ 关键帧动画(绕着圆转) ------
    // 1.做什么动画
    CAKeyframeAnimation *kAnim = [[CAKeyframeAnimation alloc] init];
    
    // 2.怎么做动画
    kAnim.keyPath = @"position";
    
    // 创建路径
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(CGRectGetWidth(self.view.bounds) * 0.5, CGRectGetHeight(self.view.bounds) * 0.5) radius:100 startAngle:0 endAngle:2 * M_PI clockwise:YES];
    
    kAnim.path = path.CGPath; // 路径
    
    // 2.操作
    group.animations = @[bAnim, kAnim];
    // 时间
    group.duration = 3;
    // 重复次数
    group.repeatCount = INT_MAX;
    
    // 3.添加动画
    [self.layer addAnimation:group forKey:nil];
}

#pragma mark - 关键帧动画(CAKeyframeAnimation)补充
- (void)keyframeAnimationAdvanceTest {
    UIView* redView = [[UIView alloc] init];
    redView.frame = CGRectMake(100, 100, 20, 20);
    redView.backgroundColor = [UIColor redColor];
    self.layer = redView.layer;
    [self.view addSubview:redView];
    
    UIView* redView1 = [[UIView alloc] init];
    redView1.frame = CGRectMake(100, 100, 20, 20);
    redView1.backgroundColor = [UIColor redColor];
    self.layer1 = redView1.layer;
    [self.view addSubview:redView1];
}

- (void)keyframeAnimationAdvance {
    // 1.做什么动画
    CAKeyframeAnimation *anim = [[CAKeyframeAnimation alloc] init];
    
    // 2.怎么做动画
    anim.keyPath = @"position";
    
    // 创建路径
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 100, 200, 200)];
    
    anim.path = path.CGPath; // 路径
    anim.duration = 2; // 时间
    anim.repeatCount = INT_MAX; // 重复次数
    
    // 3.对谁做动画
    [self.layer addAnimation:anim forKey:nil];
    
    CAKeyframeAnimation *anim1 = [[CAKeyframeAnimation alloc] init];
    
    anim1.keyPath = @"position";
    
    UIBezierPath *path1 = [UIBezierPath bezierPathWithRect:CGRectMake(100, 100, 200, 200)];
    
    anim1.path = path1.CGPath;
    anim1.duration = 2;
    anim1.repeatCount = INT_MAX;
    
    [self.layer1 addAnimation:anim1 forKey:nil];
}

#pragma mark - 关键帧动画(CAKeyframeAnimation)
- (void)keyframeAnimationTest {
    UIView *redView = [[UIView alloc] init];
    
    redView.frame = CGRectMake(100, 100, 20, 20);
    redView.backgroundColor = [UIColor redColor];
    
    self.layer = redView.layer;
    
    [self.view addSubview:redView];
}

// 关键帧动画
- (void)keyframeAnimation {
    // 1.做什么动画
    CAKeyframeAnimation *anim = [[CAKeyframeAnimation alloc] init];
    
    // 2.怎么做动画
    anim.keyPath = @"position";
    
//    NSValue* v1 = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
//    NSValue* v2 = [NSValue valueWithCGPoint:CGPointMake(150, 100)];
//    NSValue* v3 = [NSValue valueWithCGPoint:CGPointMake(100, 150)];
//    NSValue* v4 = [NSValue valueWithCGPoint:CGPointMake(150, 150)];
//
//    anim.values = @[ v1, v2, v3, v4 ]; // 关键的数据
    
    // 创建路径
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(CGRectGetWidth(self.view.bounds) * 0.5, CGRectGetHeight(self.view.bounds) * 0.5) radius:100 startAngle:0 endAngle:2 * M_PI clockwise:YES];
    
    anim.path = path.CGPath; // 动画路径
    anim.duration = 2; // 动画时间
    anim.repeatCount = INT_MAX; // 重复次数
    anim.removedOnCompletion = NO; // 是否移除动画
    
    // 3.对谁做动画
    [self.layer addAnimation:anim forKey:nil];
}

#pragma mark - 基本动画(CABasicAnimation)
- (void)basicAnimationTest {
    UIView *redView = [[UIView alloc] init];
    
    redView.frame = CGRectMake(100, 100, 100, 100);
    redView.backgroundColor = [UIColor redColor];
    
    self.layer = redView.layer;
    
    [self.view addSubview:redView];
}

// 基本动画
- (void)basicAnimation {
    // 1.创建动画对象(做什么动画)
    CABasicAnimation *anim = [[CABasicAnimation alloc] init];
    
    // 2.怎么做动画(需要修改的属性)
    anim.keyPath = @"position.x";
    
    // anim.fromValue = @(10); // 从哪
    // anim.toValue = @(300); // 到哪
    
    anim.byValue = @(50); // 在自身的基础上增加
    
    // 不希望回到原来的位置，需要设置 fillMode 属性
    anim.fillMode = kCAFillModeForwards;
    // 如果不设置removedOnCompletion那么fillMode不起作用
    anim.removedOnCompletion = NO;
    
    // 3.添加动画(对谁做动画)
    [self.layer addAnimation:anim forKey:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
